package com.it.gw.modules.user.activity


import android.annotation.SuppressLint
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.it.common.utils.isNotEmpty
import com.it.common.view.CommonViewPager2Adapter
import com.it.data.manager.AppManager
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityMainBinding
import com.it.gw.modules.user.fragment.HomeFragment
import com.it.gw.modules.user.fragment.MyFragment
import com.yili.pda.dialog.UpdateDialog


/**
 * 主页
 */
class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val fragmentList by lazy { mutableListOf<Fragment>() }


    companion object {
        const val TAG = "MainActivity"
    }

    override fun initObserver() {
        mainViewModel.versionLiveData.observe(this) {
            if (it.result && it.data.isNotEmpty()) {
                if (it.data!!.VERSION > AppManager.getAppVersionName(this)) {
                    UpdateDialog.showUpdateDialog(
                        url = it.data!!.URL,
                        versionName = it.data!!.VERSION,
                        context = this
                    )
                }

            }
        }

    }

    override fun initListener() {
        binding.llMain.setOnClickListener {
            setCurrentItem(0)
        }

        binding.llMy.setOnClickListener {
            setCurrentItem(1)
        }
        binding.vContent.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                updateUI(position)
            }

            private fun updateUI(position: Int) {
                val selectedColor = ContextCompat.getColor(this@MainActivity, R.color.color_0F6AF8)
                val unselectedColor =
                    ContextCompat.getColor(this@MainActivity, R.color.color_1C1F2E)
                val patientSelected = position == 0
                val mySelected = position == 1
                binding.ivMain.setImageResource(if (patientSelected) R.drawable.icon_main_selected else R.drawable.icon_main_unselect)
                binding.tvMain.setTextColor(if (patientSelected) selectedColor else unselectedColor)


                binding.ivMy.setImageResource(if (mySelected) R.drawable.icon_my_select else R.drawable.icon_my_unselect)
                binding.tvMy.setTextColor(if (mySelected) selectedColor else unselectedColor)
            }
        })
    }

    private fun setCurrentItem(position: Int) {
        binding.vContent.setCurrentItem(position, false)
    }

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    override fun initData() {
        mainViewModel.version()

    }


    override fun onDestroy() {
        super.onDestroy()


    }


    override fun initView() {
        binding.vContent.isUserInputEnabled = false
        fragmentList.add(HomeFragment())
        fragmentList.add(MyFragment())
        binding.vContent.adapter =
            CommonViewPager2Adapter(supportFragmentManager, lifecycle, fragmentList)
    }

    override fun setContentView() {
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}